package cn.lena.idle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.User;

/**
 * @author lena
 * @date 2021/6/11
 */
public class MyActivity extends AppCompatActivity {

    private ImageView home;
    private ImageButton head;
    private Button myself;
    private Button changePass;
    private Button myPublish;
    private Button myCollection;
    private Button logout;
    private TextView name;
    private Button mySell;
    private Button myDelete;
    private Button myBuy;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        // 初始化页面
        name=(TextView)findViewById(R.id.name);
        name.setText(LoginUser.CURRENT_USER.getUsername());
        head=(ImageButton)findViewById(R.id.imageView222);
        head.setEnabled(false);
        head.setImageBitmap(BitmapFactory.decodeByteArray(LoginUser.CURRENT_USER.getHead(),0,LoginUser.CURRENT_USER.getHead().length));

        // 我的收藏：跳转到我的收藏页面
        myCollection=(Button)findViewById(R.id.myCollection);
        myCollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,MyCollectionActivity.class);
                startActivity(intent);
            }
        });

        // 正在出售：跳转到正在出售页面
        myPublish=(Button)findViewById(R.id.myPublish);
        myPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,MyGoodsActivity.class);
                startActivity(intent);
            }
        });

        // 下架商品：跳转到下架商品页面
        myDelete=(Button)findViewById(R.id.myDelete);
        myDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,MyOffShelfActivity.class);
                startActivity(intent);
            }
        });

        // 卖出商品:：跳转到卖出商品页面
        mySell=(Button)findViewById(R.id.mySell);
        mySell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,MySellActivity.class);
                startActivity(intent);
            }
        });

        //购买记录
        myBuy=(Button)findViewById(R.id.myBuy);
        myBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,MyBuyActivity.class);
                startActivity(intent);
            }
        });


        // 个人信息：跳转到个人信息页面 t
        myself=(Button)findViewById(R.id.myInformation);
        myself.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,MyInformationActivity.class);
                startActivity(intent);
            }
        });

        // 修改密码：跳转到修改密码页面 t
        changePass=(Button)findViewById(R.id.changePass);
        changePass.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,ChangePasswordActivity.class);
                startActivity(intent);
            }

        });

        // 退出登录：退出登录，然后跳转到登录页面
        logout=(Button)findViewById(R.id.loginout);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
            }
        });

        // home图标：跳到首页
        home=(ImageView)findViewById(R.id.tohome);
        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,IndexActivity.class);
                startActivity(intent);
                MyActivity.this.finish();
            }
        });

    }


    /**
     * 退出登录 t
     */
    private void logout(){
        // 清空当前登录用户信息
        LoginUser.CURRENT_USER=new User();
        // 跳转到登录页面
        Intent intent=new Intent(MyActivity.this,LoginActivity.class);
        startActivity(intent);
        // 退出登录后应该不能再回退到该页面，因此必须进行资源释放
        MyActivity.this.finish();
    }
}
